/**
 * CS 307 Red Cross Calendar
 * @author Kevin Barlow
 * @author David Barnes
 * @author Max Cohen
 * @author Yoongu Kang
 * @author Matt Lathrop
 * @author Thomas Maley
 */

package com.cs307.redcross.client;

import java.io.Serializable;
import java.security.MessageDigest;
import java.util.Vector;

import com.google.gwt.i18n.client.DateTimeFormat;


/**
 * Stores volunteer information including their cell phone number, events they are attending,
 * their availability, etc.
 */
public abstract class Person implements Serializable {
	protected static final long serialVersionUID = 1L; // needed in order to implement Serializable
	protected String firstName; // first name of the person
	protected String lastName; // last name of the person
	protected String email; // email address of the person
	
	//TODO: make this an enum????
	protected String carrier; // carrier of the person's cell phone
	
	protected String cellPhoneNumber; // cell phone number of person
	protected String smsEmail; // email address used to send the person a text message
	
	//TODO: make this more secure
	protected String password; // password
	protected int ID;
	protected String username;
	
	protected Vector<FreeTimeEvent> freeTimeEvents;
	
	public Vector<FreeTimeEvent> getFreeTimeEvents() {
		return freeTimeEvents;
	}

	public void setFreeTimeEvents(Vector<FreeTimeEvent> freeTimeEvents) {
		this.freeTimeEvents = freeTimeEvents;
	}

	/**
	 * Constructs a blank Person object
	 */
	public Person() {
		this("", "", "", "", "", "", "", "", null);
	}
	
	/**
	 * Constructs a Person object with the given information
	 */
	public Person(String firstName, String lastName, String email, String carrier, String cellPhoneNumber, String smsEmail, String username, String password, Vector<FreeTimeEvent> freeTimeEvents) {
		this.ID = -1;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email.toLowerCase();
		this.carrier = carrier;
		this.cellPhoneNumber = cellPhoneNumber;
		this.setUsername(username);
		this.password = encrypt(password);
		this.freeTimeEvents = freeTimeEvents;
		this.smsEmail = smsEmail(this);
	}
	
	public Person(String firstName, String lastName, String email, String carrier, String cellPhoneNumber, String smsEmail, String username, String password, Vector<FreeTimeEvent> freeTimeEvents, boolean noEnc) {
		this.ID = -1;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email.toLowerCase();
		this.carrier = carrier;
		this.cellPhoneNumber = cellPhoneNumber;
		this.setUsername(username);
		this.password = password;
		this.freeTimeEvents = freeTimeEvents;
		this.smsEmail = smsEmail(this);
	}
	
	@Override
	public String toString() {
		return "Person [firstName=" + firstName + ", lastName=" + lastName
				+ ", email=" + email + ", carrier=" + carrier
				+ ", cellPhoneNumber=" + cellPhoneNumber + ", smsEmail="
				+ smsEmail + ", password=" + password + ", ID=" + ID + "]";
	}
	
	private String smsEmail(Person p){
		String car1 = p.getCarrier();
		String sms = null;
		if(car1.equals("att") || car1.equals("AT&T")) {
			sms =  (p.getCellPhoneNumber() + "@txt.att.net").toString();
		}
		else if(car1.equals("Sprint") || car1.equals("Sprint PCS")) {
			sms = (p.getCellPhoneNumber() + "@messaging.sprintpcs.com").toString();
		}
		else if(car1.equals("Verizon")) {
			sms = (p.getCellPhoneNumber() + "@xtext.com").toString();
		}
		else if(car1.equals("T-Mobile")) {
			sms = (p.getCellPhoneNumber() + "@tmomail.net").toString();
		}
		else if(car1.equals("Virgin Mobile")) {
			sms = (p.getCellPhoneNumber() + "@vmobl.com").toString();
		}
		else if(car1.equals("US Cellular")) {
			sms = (p.getCellPhoneNumber() + "@email.uscc.net").toString();
		}
		else if(car1.equals("Nextel")) {
			sms = (p.getCellPhoneNumber() + "@messaging.nextel.com").toString();
		}
		else if(car1.equals("Boost")) {
			sms = (p.getCellPhoneNumber() + "@myboostmobile.com").toString();
		}
		else if(car1.equals("Alltel")) {
			sms = (p.getCellPhoneNumber() + "@message.alltel.com").toString();
		}
		return sms;
	}
	
	static public String encrypt(String password )
	{
		 String algorithm = "SHA";

	        byte[] plainText = password.getBytes();

	        MessageDigest md = null;

	        try {		
	            md = MessageDigest.getInstance(algorithm);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
			
	        md.reset();		
	        md.update(plainText);
	        byte[] encodedPassword = md.digest();

	        StringBuilder sb = new StringBuilder();
	        for (int i = 0; i < encodedPassword.length; i++) {
	            if ((encodedPassword[i] & 0xff) < 0x10) {
	                sb.append("0");
	            }

	            sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
	        }
		return sb.toString();
	}
	/**
	 * The times of the month/week/day that the person is able to 
	 * volunteer at an event
	 */
	public void availability() {
		//TODO:
		return;
	}	
	
	/**
	 * The events that the person is currently signed up to attend
	 */
	public void eventsAttending() {
		//TODO:
		return;
	}
	
	// getters **********************************************************
	
	/**
	 * @return the first name of the person
	 */
	public String getFirstName() {
		if (firstName == null)
			return "";
		return this.firstName;
	}
	
	/**
	 * @return the last name of the person
	 */
	public String getLastName() {
		if (lastName == null)
			return "";
		return this.lastName;
	}
	
	/**
	 * @return the email address of the person
	 */
	public String getEmail() {
		return this.email;
	}
	
	/**
	 * @return the carrier that the person's cell phone is on
	 */
	public String getCarrier() {
		return this.carrier;
	}
	
	/**
	 * @return the cell phone number of the person
	 */
	public String getCellPhoneNumber() {
		return this.cellPhoneNumber;
	}
	
	/**
	 * @return the SMS email address of the person
	 */
	public String getSmsEmail() {
		return this.smsEmail;
	}
	
	/**
	 * TODO make this more secure
	 * @return the password of the person
	 */
	public String getPassword() {
		return this.password;
	}
	
	public int getID() {
		return this.ID;
	}
	
	
	// setters **********************************************************
	
	
	/**
	 * param the new first name of the person
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	/**
	 * param the new last name of the person
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	/**
	 * param the new email address of the person
	 */
	public void setEmail(String email) {
		this.email = email;
		this.username = email;
	}
	
	/**
	 * @param the new carrier that the person's cell phone is on
	 */
	public void setCarrier(String carrier) {
		this.carrier = carrier;
		this.smsEmail = smsEmail(this);
	}
	
	/**
	 * @param the new cell phone number of the person
	 */
	public void setCellPhoneNumber(String cellPhoneNumber) {
		this.cellPhoneNumber = cellPhoneNumber;
		this.smsEmail = smsEmail(this);
	}
	
	/**
	 * @return the SMS email address of the person
	 */
/*	public void setSmsEmail(String smsEmail) {
		this.smsEmail = smsEmail;
	}*/
	
	public String printFreeTimeEvents() {
		if (freeTimeEvents == null || freeTimeEvents.isEmpty())
			return "This person has no free time.";
		
		String returnString = "";
		
		for (FreeTimeEvent fte : freeTimeEvents) {
			if (fte == null)
				continue;
			
			returnString += DateTimeFormat.getFormat("EEE").format(fte.startTime);
			returnString += " " + DateTimeFormat.getFormat("hh:mm").format(fte.startTime);
			returnString += " to " + DateTimeFormat.getFormat("hh:mm").format(fte.endTime);
			returnString += "\n";
		}
		return returnString;
	}
	
	/**
	 * TODO make this more secure
	 * @param the password of the person
	 */
	public void setPassword(String password) {
		this.password = encrypt(password);
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
	public void setId(int id) {
		this.ID = id;
	}

}
